{
Alisa_Barkar
25/10/11
}
program plusic;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  pelement=^element;
  element=record
    value:integer;
    value1:integer;
    value2:integer;
    value3:integer;
    stepen1:integer;
    stepen2:integer;
    stepen3:integer;
    next:pelement;
  end;
  function  DeleteFromBegin (var head3:pelement):integer;
  var
    tmp3:pelement;
  begin
    if (head3<>nil) then
    begin
      tmp3:=head3;
      DeleteFromBegin:=head3^.value3;
      dispose (head3);
      head3:= tmp3;
    end;
  end;
  procedure AddToEnd (head3:pelement;sum:integer;p:integer);
  var
    tmp3:pelement;
  begin
    tmp3:=head3;
    new (head3);
    head3^.value3:=sum;
    head3^.stepen3:=p;
    head3^.next:=tmp3;
  end;
  procedure FirstAddToBegin (head1:pelement;num:integer;s:integer);
  var
    tmp1:pelement;
  begin
    tmp1:=head1;
    new (head1);
    head1^.value1:=num;
    head1^.stepen1:=s;
    head1^.next:=tmp1;
  end;
  procedure SecondAddToBegin (head2:pelement;num:integer;s:integer);
  var
    tmp2:pelement;
  begin
    tmp2:=head2;
    new (head2);
    head2^.value2:=num;
    head2^.stepen2:=s;
    head2^.next:=tmp2;
  end;
  function OnePlusTwo (head1:pelement;head2:pelement;head3:pelement):integer;
  var
    tmp1, tmp2:pelement;
    sum, p:integer;
  begin
    tmp1:=head1;
    tmp2:=head2;
    while (tmp1<>nil) and (tmp2<>nil) do
    begin
      if (tmp1^.stepen1=tmp2^.stepen2) then
      begin
        sum:=tmp1^.value1+tmp2^.value2;
        p:=tmp1^.stepen1;
        AddToEnd (head3, sum, p);
        tmp1:=tmp1^.next;
        tmp2:=tmp2^.next;
      end;
      if (tmp1^.stepen1>tmp2^.stepen2) then
      begin
        sum:=tmp1^.value1;
        p:=tmp1^.stepen1;
        AddToEnd (head3, sum, p);
        tmp1:=tmp1^.next;
      end;
      if (tmp1^.stepen1<tmp2^.stepen2) then
      begin
        sum:=tmp2^.value2;
        p:=tmp2^.stepen2;
        AddToEnd (head3, sum, p);
        tmp2:=tmp2^.next;
      end;
    end;
    if (tmp1=nil) and (tmp2<>nil) then
    begin
      while (tmp2<>nil) do
      begin
        sum:=tmp2^.value2;
        p:=tmp2^.stepen2;
        AddToEnd (head3, sum, p);
        tmp2:=tmp2^.next;
      end;
    end;
    if (tmp2=nil) and (tmp1<>nil) then
    begin
      while (tmp1<>nil) do
      begin
        sum:=tmp1^.value1;
        p:=tmp1^.stepen1;
        AddToEnd (head3, sum, p);
        tmp1:=tmp1^.next;
      end;
    end;
  end;
  procedure PrintList (head3:pelement);
  var
    current:pelement;
  begin
    current:=head3;
    while (current<>nil) do
    begin
      writeln (current^.value);
      current:=current^.next;
    end;
  end;
var
  head1, head2, head3:pelement;
  mass1, mass2, step1, step2:array of integer;
  n, m, i, r, j:integer;
  fin:textFile;
begin
  assignFile (fin, 'input.txt');
  reset (fin);
  readln (fin, n);
  setlength (mass1, n);
  setlength (step1, n);
  for i:=0 to n-1 do
  begin
    read (fin, mass1[i]);
    readln (fin, step1[i]);
  end;
  readln (fin, m);
  setlength (mass2, m);
  setlength (step2, m);
  for i:=0 to m-1 do
  begin
    read (fin, mass2[i]);
    readln (fin, step2[i]);
  end;
  closeFile (fin);
  for i:=n-1 downto 0 do
    FirstAddToBegin (head1, mass1[i], step1[i]);
  for i:= m-1 downto 0 do
    SecondAddToBegin (head2, mass2[i], step2[i]);
  OnePlusTwo (head1, head2, head3);
  PrintList (head3);
  r:=0;
  for i:= 0 to m-1 do
    for j:=0 to n-1 do
      if (step1[j]=step2[i]) then
        r:=r+1;
  if (n>m) then
  begin
    for i:=0 to n+r do
      DeleteFromBegin (head3);
  end else begin
    for i:=0 to m+r do
      DeleteFromBegin (head3);
  end;
  readln;
end.
 